﻿

using HBase; 
using hCommand;

using hCommand.dto;
using hCommand.entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace api
{
    //[FormAuthenticationFilter]
    [RoutePrefix("api/file")]
    public class FileController :BaseApiController {
        ISysServer sysAuthorityServer = WMSDalFactray.getDal<ISysServer>();
        [HttpGet, Route("download/{id}")]
        public object Get(String id) {
            String path = System.Web.HttpUtility.UrlDecode(id).Replace('@' ,'/');

            var f = new FileInfo(FileCache.Path + path);
            if( !f.Exists ) {
                return BaseResult.Error("NO_FUND_ERROR");
            }
            return new FileStreamResult(f.OpenRead() ,path);
        }

        [HttpPost, Route("filePage")]
        public BaseResult filePage([FromBody] FileDto model) {
            var v = sysAuthorityServer.queryFilePage(model);
            return BaseResult.Ok(v);
        }

        [HttpPost, Route("fileSave")]
        public BaseResult fileSave([FromBody] SysFile file) {


            file.status = 1;
            file.tableName = "m_equipment";
            if( String.IsNullOrEmpty(file.type) ) {
                file.type = "video";
            }

            if (file.id == 0) {
                file.id = WMSIdUtil.getId();
                sysAuthorityServer.saveFile(file);
            } else {
                return BaseResult.Error("附件异常");
            }

            return BaseResult.Ok("信息保存");

        }

        [HttpPost, Route("upload")]
        public BaseResult Upload([FromBody] List<String> file) {
            return new BaseResult(file);
        }

        [HttpPost, Route("list/{pid}")]
        public BaseResult Upload(long pid) {
            var v = sysAuthorityServer.getFile(pid);
            return new BaseResult(v);
        }

    }
}
